var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();

Page({
  data: {
    util: util,
    orgInfo: '',
    org_name: '',
    isIphone_x: false,
    screen_h: 0,
    screen_w: 0,

    userListData: [],

    showVideo: false,
    roomDatas:[],

    selTitleIndex: 1
  },

  cuid: 0,
  token: '',
  openid: '',

  lookmeUserList: '',
  melookUserList: '',

  onLoad: function (data) {
    console.log("screen_w = " + app.screen_w)
    this.setData({
      isIphone_x: app.isIphone_x,
      screen_w: app.screen_w,
      screen_h: app.screen_h,
    });

    var that = this
    app.LoginUserInfo(function(res){
      if(res && res != '-1' && res != '-2'){
        that.cuid = res.uid
        that.token = res.token
        that.openid = res.openid
      }
    })
    
    app.getWxCheck(function(show) {
      that.setData({
        showVideo: show
      })
      if(show) {
        that.getLookUserList(1)
      }
    }, true)
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  onShow: function () {
    this.getLoginUserInfo(false);
  },

  selectTitleItem: function (params) {
    let selIndex = params.currentTarget.dataset.index
    this.setData({
      selTitleIndex: selIndex
    })
    let navTitle = '谁看过我'
    if (selIndex == '2') {
      navTitle = '我看过谁'
    }
    this.changeTitleAction()
    tt.setNavigationBarTitle({
      title: navTitle
    })
  },

  changeTitleAction: function () {
    if (this.data.selTitleIndex == '1' && this.lookmeUserList && this.lookmeUserList.length > 0) {
        this.setData({
          userListData: this.lookmeUserList
        })
        return
    } else if (this.data.selTitleIndex == '2' && this.melookUserList && this.melookUserList.length > 0) {
      this.setData({
        userListData: this.melookUserList
      })
      return
    }

    this.getLookUserList(1)
  },

  getLookUserList: function(pageIndex) {
    var that = this
    if (!that.data.showVideo) return

    var acStr = 'getwholooklist'
    if (that.data.selTitleIndex == '2') {
      acStr = 'getmylooklist'
    }
    util.api_go({
        data: {
          "publickey": util.publiccode,
          "ac": acStr,
          "uid":that.cuid,
          "page": pageIndex,
          "num": 100
        },
        success: function (responseData) {
          console.log("look列表：", responseData);
          if(!responseData.data.msg){
            let resultData = responseData.data
            resultData.forEach(function (m) {
              let timeInt = parseInt(m.dateline)
              let timeStr = util.formatTime(new Date(timeInt*1000), '-', ':', false)
              m.timeStr = timeStr.slice(2, timeStr.length)
            })
            if (pageIndex > 1) {
              resultData = that.data.userListData.concat(responseData.data)
            }
            that.setData({
              userListData: resultData
            })
            if (that.data.selTitleIndex == '1') {
              that.lookmeUserList = resultData
            } else {
              that.melookUserList = resultData
            }
          } else {
            tt.showToast({
              title: responseData.data.msg,
            })
          }
        }
      });
  },

  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    tt.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },

  goAtherPage: function (e) {
    console.log("goAtherPage!!!!!!!!");
    var url = e.currentTarget.dataset.link;
    tt.navigateTo({
      url: url
    })
  },
  goAtherPageRedirect: function (e) {
    var url = e.currentTarget.dataset.link;
    tt.redirectTo({
      url: url
    })
  },

  //底部导航跳转
  navigtionClick: function (e) {
    console.log("点击参数：", e);
    var path = e.currentTarget.dataset.path;
    console.log("跳转连接信息：", path);
    if (path) {
      if (path.indexOf('navto:') == 0) {
        var navPath = path.slice(6);
        tt.navigateTo({
          url: navPath,
        })
      } else {
        tt.redirectTo({
          url: path
        });
      }
    }
  },

  allYuyueKcIds: '',
  getLoginUserInfo: function (reAuth) {
    var that = this;
    
    //读取登录用户信息
    app.LoginUserInfo(function (res) {
      console.log("登录用户信息：", res);
      if (res && res != -1) {
        that.setData({
          loginuid: res.uid,
          token: res.token,
          userInfo: res
        });
      }
    }, reAuth);
  },

  sendHelloMessage: function (e) {
    // post请求，ac=postmsg，传入uid-发送者uid，touid-接收者uid，msg-消息内容。返回操作成功/失败。调用成功后 会增加消息列表接口中的未读数量
    var toUid = e.currentTarget.dataset.uid
    var that = this;
    if (!that.cuid || that.cuid == '0') {
      return
    }
    let noGetSubMsg = app.noSubscribeMessageCache()
    console.log("已经授权订阅消息：" + noGetSubMsg)
    if (noGetSubMsg) {
      tt.requestSubscribeMessage({
        tmplIds: [app.globalData.tmplMsgIds],
        success(res) {
          console.log("订阅消息结果：", res)
          if (res.errMsg.indexOf(":ok") > 0) {
            app.setSubscribeMessageCache()
          }
        }
      })
    }
    tt.navigateTo({
      url: '/pages/chatMessage/chatMessage?sayhello=true&uid=' + toUid
    })
  },


})